import threading
from time import sleep

'''
线程是可以共享全局变量的
GIL：全局解释器锁
    1.保证同一时刻只有一个线程在运行
    2.保证线程安全
    3.但是会导致多核CPU无法充分利用
    4.多核CPU：多进程+多线程
    5.单核CPU：多线程
    
'''
# money = 1000
#
# def run1():
#     global money
#     for i in range(100):
#         money -= 1
#
# def run2():
#     global money
#     for i in range(100):
#         money -= 1
#
# if __name__ == '__main__':
#     t1 = threading.Thread(target=run1,name='th1')
#     t2 = threading.Thread(target=run2,name='th2')
#
#     t1.start()
#     t2.start()
#
#     t1.join()
#     t2.join()
#     print('money:',money)
print('-------------------------------')
trick = 1000

def run1():
    global trick
    for i in range(100):
        sleep(0.1)
        trick -= 1

if __name__ == '__main__':
    t1 = threading.Thread(target=run1,name='th1')
    t2 = threading.Thread(target=run1,name='th2')
    t3 = threading.Thread(target=run1,name='th3')
    t4 = threading.Thread(target=run1,name='th4')

    t1.start()
    t2.start()
    t3.start()
    t4.start()

    t1.join()
    t2.join()
    t3.join()
    t4.join()
    print('money:',trick)
